package com.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.web.config.Constants;
import com.web.config.PayConfiguration;
import com.web.controller.pay.*;
import com.web.dao.TechInfoMapper;
import com.web.entity.TechInfo;
import com.web.entity.UserCertification;
import com.web.entity.UserInfo;
import com.web.entity.UserMargin;
import com.web.service.*;
import com.web.service.LoginService;
import com.web.utils.AliSMSUtil;
import com.web.utils.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class BaseController {
    @Autowired
    public LoginService infoService;
    @Autowired
    public StringRedisTemplate redisTemplate;
    @Resource
    public AuthenticationService authenticationService;
    @Resource
    public TechInfoMapper techInfoMapper;
    @Autowired
    private PayConfiguration payConfiguration;
    /**
     * 根据某个值获取用户信息
     * @param key
     * @param value
     * @return
     */
    public UserInfo findByUser(String key, String value){
        return infoService.info(key,value);
    }

    public UserCertification selectCertification(String userId){
        return infoService.selectCertification("user_id",userId);
    }

    /**
     * 验证码发送封装类
     * @param phone
     * @param type
     * @return
     * @throws ClientException
     */
    public boolean getVcodes(String phone, String type) throws ClientException {
        String code="888888";
        //String code = String.valueOf((int) ((Math.random()*9+1)*100000));
        System.err.println(code);
        redisTemplate.opsForValue().set(type+phone,code,10, TimeUnit.MINUTES);//将验证码存入redis缓存服务器
        SendSmsResponse response = AliSMSUtil.sendSms(phone,code);
        if (response.getCode().equals("OK")){
            redisTemplate.opsForValue().set(Constants.APP_NAME+type+phone,code,10, TimeUnit.MINUTES);//将验证码存入redis缓存服务器
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取金额比例信息
     * @return
     */
    public UserMargin getMarginInfo(){
        return infoService.getMarginInfo();
    }

    /**
     * 查看用户是否认证
     * @param userid
     * @return
     */
    public int  getUserCertification(String userid){
        UserCertification userCertification = infoService.selectCertification("user_id",userid);
        if(userCertification==null){
            return 2;
        }
        if(userCertification.getType() == null && userCertification.getRealname() == null &&
                userCertification.getIdCard() == null && userCertification.getCardZ() == null &&
                userCertification.getCardF() == null && userCertification.getTechId() == null &&
                userCertification.getTechCard() == null && userCertification.getAuthStatus() == 2){

            return 2;
        }
        return 1;

    }

    /**
     * 查询企业是否完善
     * @param userid
     * @return
     */
    public int getTechInfo(String userid){
        TechInfo techInfo = techInfoMapper.findTechInfo("user_id",userid);
        if(techInfo == null){
            return 2;
        }

        if(techInfo.getId()==null && techInfo.getUserId() == null && techInfo.getLogo()==null  &&
           techInfo.getCity() ==null && techInfo.getBusiness() == null && techInfo.getIntroduction() == null &&
                techInfo.getTechPhotos() == null && techInfo.getDevicePhotos()==null ){
            return 2;
        }
        return 1;
    }

    public Object AliPay(String outtradeno,String body,Double price){
        AliPayDto aliPayDto = new AliPayDto();
        aliPayDto.setPayAppid(payConfiguration.getAli().getAppid());
        aliPayDto.setPayUrl(payConfiguration.getAli().getUrl());
        aliPayDto.setPaySelfSecret(payConfiguration.getAli().getSelfSecret());
        aliPayDto.setPayPublicSecret(payConfiguration.getAli().getPublicSecret());
        aliPayDto.setPayPublicSecret2(payConfiguration.getAli().getPublicSecret2());
        aliPayDto.setOutTradeNo(outtradeno);
        aliPayDto.setSubject(body);
        aliPayDto.setTotalAmount(String.valueOf(price));
        aliPayDto.setBody(body);
        aliPayDto.setNotifyUrl(payConfiguration.getAli().getNotifyUrl());
        PayResponse response = AliPayManage.mg.aliPayApp(aliPayDto);
        if (response.isOk()){
            return response.getParams();
        }
        return null;
    }

    public Object WeChatPay(String outtradeno,String body,Double price){
        WxPayDto wxPayDto = new WxPayDto();
        wxPayDto.setAppid(payConfiguration.getWx().getAppId());
        wxPayDto.setMch_id(payConfiguration.getWx().getMchId());
        wxPayDto.setSignKey(payConfiguration.getWx().getSignKey());
        wxPayDto.setTrade_type("APP");
        wxPayDto.setTotal_fee(String.valueOf((int) DoubleUtil.round(price * 100, 0)));
        wxPayDto.setBody(body);
        wxPayDto.setOut_trade_no(outtradeno);
        wxPayDto.setSpbill_create_ip("183.129.227.58");
        wxPayDto.setNotify_url(payConfiguration.getWx().getNotfiyUrl());
        PayResponse response = WxPayManage.mg.wxPay(wxPayDto);
        if (response.isOk()){
            return response.getParams();
        }
        return null;
    }



}
